<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>business_add</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <style>
        .view-form .layui-input-block, .view-form .layui-input-inline {
            line-height: 36px;
            background: #eeeeee;
            padding-left: 20px;
            font-size: 16px;
        }
    </style>
</head>

<body>

<div style="padding: 20px;">
    <form class="layui-form" action="">
        <input id="ID" name="ID" type="hidden"/>
        <div class="layui-form-item" id="fjm" style=" height: 45px;">
            <label class="layui-form-label" style="height: 50px;width: 120px;">项目类型名称：</label>
            <div class="layui-input-inline" id="fjmc">
                <input type="text" name="NAME" lay-verify="required" placeholder="请输入类型名称" autocomplete="off" class="layui-input" >
            </div>
        </div>

        <div class="layui-form-item" id="kg" style="padding-left: 30px;margin-bottom: 30px;">
            <label class="layui-form-label" style="padding-left: 0px;padding-right: 10px;width: 130px;">是否纳入楼宇面积：</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest"
                       lay-text="启用|关闭">
            </div>
        </div>

        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block" >
                <button class="layui-btn" lay-submit="" lay-filter="room">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="../../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    layui.use(['jquery', 'form', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer;
        //操作类型：add新增，edit修改
        var operateType = QueryUtils.GetQueryString("type");
        var id = QueryUtils.GetQueryString("id");
        $("#ID").val(id);
        switch (operateType) {
            case "add":
                break;
            case "edit":
                $.ajax({
                    url: "/dictionary/infotype",
                    data: {ID:id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $("input[name='NAME']").val(res.data.NAME);
                            /*$("input[name='NAME']").parent().html(res.data.NAME)*/
                            if (res.data.IS_OPEN === "on") {
                                $("input[name='IS_OPEN']").parent().html('<input type="checkbox" checked="" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">');
                            } else if (res.data.IS_OPEN === "off") {
                                $("input[name='IS_OPEN']").parent().html('<input type="checkbox" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">');
                            }
                            form.render();
                        }else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
        }


        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            if (this.checked === true) {
                layer.tips('温馨提示：已启用' + '', data.othis);
                data.othis.parent().parent().attr("data-open", "on");
            } else if (this.checked === false) {
                layer.tips('温馨提示：已关闭' + '', data.othis);
                data.othis.parent().parent().attr("data-open", "off");
            }
        });


        //监听提交
        form.on('submit(room)', function (data) {

            if (!data.field.IS_OPEN) {
                data.field.IS_OPEN = "off"; //如果是否开启为未开启则设置为off
            }

            if (operateType === "add") {
                    layer.msg('确认保存新增项目类型吗', {
                        time: 0
                        , btn: ['确定', '取消']
                        , shade: 0.3
                        , yes: function (index) {
                            layer.close(index); // 关闭当前提示框
                            $.ajax({
                                url: "/dictionary/savetype",
                                data: data.field,
                                dataType: "json",
                                type: "post",
                                async: false,
                                success: function (res) {
                                    if (res.errcode === "0"){
                                        var i = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(i);  // 关闭新建房间页面
                                        parent.reload();    // 父页面表格刷新
                                        parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                    } else if (res.errcode === "1"){
                                        layer.msg(res.errmsg, function(){});
                                    }
                                },
                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                    console.error(XMLHttpRequest.status);
                                    console.error(XMLHttpRequest.readyState);
                                    console.error(textStatus);
                                }
                            });
                        }
                    });
            } else if (operateType === "edit") {

                layer.msg('确认编辑新增项目类型吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/dictionary/edittype",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "0") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭修改房间页面
                                    parent.reload();    // 父页面表格刷新
                                    parent.layer.msg("修改成功", {offset: '150px'});   // 父页面弹出修改成功
                                } else if (res.errcode === "1") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });
    });
</script>
</body>
</html>